import { createStore, ActionContext } from 'vuex';
interface IState {
  name: string;
  age: number;
  city: string;
}

export default createStore<IState>({
  state() {
    return {
      name: 'comyan',
      age: 34,
      city: 'aaa',
    } as IState;
  },
  mutations: {
    changeName(state: IState, payload: string) {
      state.name = payload;
    },
    addNumber(state: IState, payload: number) {
      state.age += payload;
    },
  },
  getters: {
    doubleCount(state: IState) {
      return state.age * 2;
    },
  },
  actions: {
    asyncAddNumber({ commit }: ActionContext<IState, IState>) {
      setTimeout(() => {
        commit('addNumber', 30);
      }, 2000);
    },
  },
  modules: {
    moduleA: {
      namespaced: true,
      state() {
        return {
          likes: ['eating', 'running'],
        };
      },
      mutations: {
        addLikes(state, payload) {
          state.likes.push(payload);
        },
      },
      actions: {
        asyncAddLikes({ commit }, payload) {
          setTimeout(() => {
            commit('addLikes', payload);
          }, 2000);
        },
      },
    },
    moduleB: {},
  },
});
